package com.group1.bookmanagement.action;

import java.io.ByteArrayInputStream;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.group1.bookmanagement.util.IdentityUtils;
import com.opensymphony.xwork2.ActionSupport;

@Controller("validCodeAction")
@Scope("prototype")
public class ValidCodeAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	// 图片流
	private ByteArrayInputStream imageStream;

	@Override
	public String execute() throws Exception {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			String code = IdentityUtils.getRandomString();
			imageStream = IdentityUtils.getImageAsInputStream(code);
			// 放入session中
			request.getSession().setAttribute("code", code);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return SUCCESS;
	}

	public ByteArrayInputStream getImageStream() {
		return imageStream;
	}

	public void setImageStream(ByteArrayInputStream imageStream) {
		this.imageStream = imageStream;
	}
	
}
